/*
 * Copyright 2005-2013 finalist.cn. All rights reserved.
 * Support: http://www.finalist.cn
 * License: http://www.finalist.cn/license
 */
package com.finalist.controller.admin;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.finalist.Filter;
import com.finalist.Message;
import com.finalist.Pageable;
import com.finalist.ResourceNotFoundException;
import com.finalist.Setting;
import com.finalist.Filter.Operator;
import com.finalist.entity.Admin;
import com.finalist.entity.Product;
import com.finalist.entity.Review;
import com.finalist.entity.Review.Type;
import com.finalist.service.AdminService;
import com.finalist.service.ReviewService;
import com.finalist.util.SettingUtils;

/**
 * Controller - 评论
 * 
 * @author Finalist IT Group
 * @version 3.0
 */
@Controller("adminReviewController")
@RequestMapping("/admin/review")
public class ReviewController extends BaseController {

	@Resource(name = "reviewServiceImpl")
	private ReviewService reviewService;
	
	@Resource(name = "adminServiceImpl")
	private AdminService adminService;
	
	/**
	 * 编辑
	 */
	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	public String edit(Long id, ModelMap model) {
		model.addAttribute("review", reviewService.find(id));
		return "/admin/review/edit";
	}

	/**
	 * 更新
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Long id, @RequestParam(defaultValue = "false") Boolean isShow, RedirectAttributes redirectAttributes) {
		Review review = reviewService.find(id);
		if (review == null) {
			return ERROR_VIEW;
		}
		review.setIsShow(isShow);
		reviewService.update(review);
		addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
		return "redirect:list.jhtml";
	}

	/**
	 * 列表
	 */
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Type type, Pageable pageable, ModelMap model) {
		model.addAttribute("type", type);
		model.addAttribute("types", Type.values());
		
		Admin admin=adminService.getCurrent();
		
		if(admin.getAminId()!=null){
			List<Filter> filters = new ArrayList<Filter>();
			Filter filter=new Filter();
			filter.setOperator(Operator.eq);
			filter.setProperty("adminId");
			filter.setValue(adminService.getCurrent().getId());
			filters.add(filter);
			pageable.setFilters(filters);
		}
		
		model.addAttribute("page", reviewService.findPage(null, null, type, null, pageable));
		return "/admin/review/list";
	}

	/**
	 * 删除
	 */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody
	Message delete(Long[] ids) {
		reviewService.delete(ids);
		return SUCCESS_MESSAGE;
	}
	
	/**
	 * 评论内容
	 */
	@RequestMapping(value = "/content", method = RequestMethod.GET)
	public String content(Long id, Integer pageNumber, ModelMap model) {
		Setting setting = SettingUtils.get();
		if (!setting.getIsReviewEnabled()) {
			throw new ResourceNotFoundException();
		}
		Review review = reviewService.find(id);
		Product product = review.getProduct();
		if (product == null) {
			throw new ResourceNotFoundException();
		}
		model.addAttribute("product", product);
		model.addAttribute("review", review);
		return "/admin/review/content";
	}

}